<?php

namespace App\Admin\Controllers;

use App\Admin\Exports\MsgLogExport;
use App\Http\Controllers\Controller;
use App\Model\UserMsgLog;
use App\Model\UserMsgLogTmp;
use App\Model\UserProfile;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;

class MsgLogController extends Controller
{
    use ModelForm;

    public function index()
    {
        // DB::listen(function ($query) {
        //     dump($query->sql);
        // });
        $content = new Content();
        $content->header('聊天消息记录');
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {
        $validType = [
            UserMsgLog::RONG_TEXT_TYPE,
            UserMsgLog::RONG_SOUND_TYPE,
            UserMsgLog::RONG_IMG_TYPE,
            UserMsgLog::RONG_VIDEO_TYPE
        ];
//        if (
//            !empty(request()->to_userid)
//            || !empty(request()->from_userid)
//            || env('SHOW_ORIGIN_MSG_LOG', 0) == 1
//        ) {
//            // request()->merge(['to_userid' => 276]);
//            $grid = new Grid(new UserMsgLog());
//            $grid->model()->orderBy('id', 'desc');
//        } else {
//            $grid = new Grid(new UserMsgLogTmp());
//            $where = [
//                ['from_userid', '>', 0],
//            ];
//            $grid->model()->where($where)->orderBy('id', 'desc');
//        }

        $grid = new Grid(new UserMsgLog());
        $grid->model()->orderBy('id', 'desc');



        //->whereIn('msg_type', $validType)
        $grid->id('消息ID');
        $grid->fromProfile()->user_profile_id('发送方ID');
        $grid->from_userid('发送方内部ID');
        $grid->fromProfile()->nick_name('发送方昵称');
        $grid->fromProfile()->role('发送方角色')->display(function ($data) {
            return UserProfile::ROLE_HOST == $data ? '女用户' : '用户';
        });
        $grid->fromProfile()->gender('发送方性别')->display(function ($data) {
            return 1 == $data ? '男' : '女';
        });
        $grid->toProfile()->user_profile_id('接收方ID');
        $grid->to_userid('接收方内部ID');
        $grid->toProfile()->nick_name('接收方昵称');
        $grid->toProfile()->role('接收方角色')->display(function ($data) {
            return UserProfile::ROLE_HOST == $data ? '女用户' : '用户';
        });
        $grid->msg_type('消息类型')->display(function ($data) {
            return UserMsgLog::MSG_TYPE[$data] ?? '其他类型';
        });
        $grid->msg_detail('消息内容')->display(function ($data) {
            $msgDetail = UserMsgLog::unpackDetail($data, $this->msg_type);
            switch ($this->msg_type) {
                case UserMsgLog::RONG_TEXT_TYPE:
                    return $msgDetail['content'];
                case UserMsgLog::RONG_SOUND_TYPE:
                    return ComUtil::buildAudio($msgDetail['content']);
                case UserMsgLog::RONG_IMG_TYPE:
                    return ComUtil::buidlImgaeHtml($msgDetail['content']);
                case UserMsgLog::RONG_VIDEO_TYPE:
                    return ComUtil::buildAudio($msgDetail['content'], 'video');
            }
            return $msgDetail['content'];
        });
        $grid->created_at('发送时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        // $grid->disableExport();
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->exporter(new MsgLogExport($grid));
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('msg_type', '消息类型')->select(UserMsgLog::MSG_TYPE);
                $filter->equal('fromProfile.gender', '发送方性别')->select([1 => '男', 0 => '女']);
                $filter->equal('fromProfile.role', '发送方角色')->select([1 => '普通用户', 2 => '认证女用户']);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('from_userid', '发送方内部ID');
                $filter->equal('fromProfile.user_profile_id', '发送方ID');

            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('to_userid', '接收方内部ID');
                $filter->equal('toProfile.user_profile_id', '接收方ID');
            });
        });
        $grid->disableActions();
        $grid->actions(function (Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
        });
        return $grid;
    }
}
